class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        long long hash[100010]={0};long long count=0;long long ret=0;
        long long left=0;long long right=0;long long n=fruits.size()-1;
        while(right<=n)
        {
            //进窗口
            if(hash[fruits[right]]==0) count++;
            hash[fruits[right]]++;
            //判断
            while(count>2)
            {
                hash[fruits[left]]--;
                if(hash[fruits[left]]==0) count--;
                left++;
            }
            //更新状态
            ret=max(ret,right-left+1);
            right++;
        }
        return ret;
    }
};